// pages/draw-lots/draw-lots.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    angle: 0,
    diffX: 0,
    diffY: 0,
    diffZ: 0,
    shakeData: {
      x: 0,
      y: 0,
      z: 0
    },
    staticImgUrl: '',
    shakeInfo: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      model: app.globalData.model,
      staticImgUrl: app.globalData.staticImgUrl
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.animation = wx.createAnimation()
    this.animationBg = wx.createAnimation()
    this.shake()
  },
  shake () {
    // this.data.shakeCount = 0
    // 启用摇一摇
    this.gravityModalConfirm(true)
    wx.startAccelerometer({
      interval: 'normal'
    }) 
    wx.onAccelerometerChange((res) => {
      let x = res.x.toFixed(4),
        y = res.y.toFixed(4),
        z = res.z.toFixed(4)
      let diffX_obj = this.getDelFlag(x, this.data.shakeData.x),
        diffY_obj = this.getDelFlag(y, this.data.shakeData.y),
        diffZ_obj = this.getDelFlag(z, this.data.shakeData.z),
        flagX = diffX_obj.flag,
        flagY = diffY_obj.flag,
        flagZ = diffZ_obj.flag
      this.setData({
        diffX: diffX_obj.diff,
        diffY: diffY_obj.diff,
        diffZ: diffZ_obj.diff,
        shakeData: {
          x: x,
          y: y,
          z: z
        }
      })
      this.startAnimation()
      if ((flagX && flagY) || (flagX && flagZ)) {
        if (this.data.shakeInfo.enabled){
          this.data.shakeInfo.enabled = false
          this.playShakeAudio()
        }
      }
    })
  },
  // 启用或者停用摇一摇功能
  gravityModalConfirm(flag) {
    if (flag !== true) {
      flag = false
    }
    this.setData({
      shakeInfo: {
        num: 0,
        enabled: flag
      }
    })
  },
  playShakeAudio() {
    console.log('ok')
    wx.stopAccelerometer()
    wx.vibrateLong()
    setTimeout(()=>{
      wx.redirectTo({
        url: '../result/result'
      })
    }, 1000)
  },
  /**
   * 动画效果
   */
  startAnimation() {
    let angle = this.data.diffX * 10
    let angleBg = this.data.diffX * 1
    this.setData({
      angle: angle
    })
    if (Math.abs(angle) > 15){
      if (angle < 0) {
        angle = -15
      } else {
        angle = 15
      } 
    }
    if (Math.abs(angle) > 3) {
      this.animation.rotate(angle).step({
        transformOrigin: '50% 80% 0',
        duration: 50
      })
      this.animation.rotate(0).step({
        transformOrigin: '50% 80% 0',
        duration: 50
      })
      this.animation.rotate(-angle).step({
        transformOrigin: '50% 80% 0',
        duration: 50
      })
      this.animation.rotate(0).step({
        transformOrigin: '50% 80% 0',
        duration: 50
      })
      this.animation.rotate(10).step({
        transformOrigin: '50% 80% 0',
        duration: 35
      })
      this.animation.rotate(0).step({
        transformOrigin: '50% 80% 0',
        duration: 35
      })
      this.animation.rotate(-10).step({
        transformOrigin: '50% 80% 0',
        duration: 35
      })
      this.animation.rotate(0).step({
        transformOrigin: '50% 80% 0',
        duration: 35
      })
      this.animation.rotate(5).step({
        transformOrigin: '50% 80% 0',
        duration: 20
      })
      this.animation.rotate(0).step({
        transformOrigin: '50% 80% 0',
        duration: 20
      })
      this.animation.rotate(-5).step({
        transformOrigin: '50% 80% 0',
        duration: 20
      })
      this.animation.rotate(0).step({
        transformOrigin: '50% 80% 0',
        duration: 20
      })
      this.setData({ animation: this.animation.export() })
    }
  },

  /**
   * 计算摇一摇的偏移量
   */
  getDelFlag(val1, val2) {
    return {
      flag: (Math.abs(val1 - val2) >= 1.5),
      diff: val2 - val1
    }
  }
})